package org.openbpm.form.service;

import com.sec.etech.sys.util.ContextUtil;
import org.openbpm.base.manager.BaseService;
import org.openbpm.base.manager.impl.BaseServiceImpl;
import org.openbpm.form.mapper.FormCustChart2Mapper;
import org.openbpm.form.model.FormCustChart2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FormCustChart2Service extends BaseServiceImpl<FormCustChart2Mapper, FormCustChart2> implements BaseService<FormCustChart2> {

    @Autowired
    private FormCustChart2Mapper mapper;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /*@Override
    public boolean save(FormCustChart2 entity) {

        return num>0 ? true : false;
    }*/

    @Override
    public boolean updateById(FormCustChart2 entity) {
        StringBuffer sqlBuf = new StringBuffer("update form_cust_chart set update_time_=now(),");
        List<Object> params = new ArrayList<>();
        if(ContextUtil.getCurrentUserId()!=null) {
            params.add(ContextUtil.getCurrentUserId());
            sqlBuf.append("update_by_=?,");
        }
        if(entity.getName()!=null) {
            params.add(entity.getName());
            sqlBuf.append("name_=?,");
        }
        if(entity.getCode()!=null) {
            params.add(entity.getCode());
            sqlBuf.append("code_=?,");
        }
        if(entity.getType()!=null) {
            params.add(entity.getType());
            sqlBuf.append("type_=?,");
        }
        if(entity.getCharts()!=null) {
            params.add(entity.getCharts());
            sqlBuf.append("charts_=?,");
        }
        if(entity.getDataSource()!=null) {
            params.add(entity.getDataSource());
            sqlBuf.append("data_source_=?,");
        }
        if(entity.getBizId()!=null) {
            params.add(entity.getBizId());
            sqlBuf.append("biz_id_=?,");
        }
        if(entity.getExpand()!=null) {
            params.add(entity.getExpand());
            sqlBuf.append("expand_=?,");
        }
        if(entity.getCondition()!=null) {
            params.add(entity.getCondition());
            sqlBuf.append("condition_=?,");
        }
        if(entity.getRemark()!=null) {
            params.add(entity.getRemark());
            sqlBuf.append("remark_=?,");
        }
        if(params.size()==0) {
            return false;
        }
        String sql = sqlBuf.toString().substring(0,sqlBuf.length()-1) + " where id_=?";
        params.add(entity.getId());
        int num = jdbcTemplate.update(sql, params.toArray());
        return num>0 ? true : false;
    }
}
